package com.live.dengdengtv.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppConstants;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;
import com.live.dengdengtv.wxapi.WXPayEntryActivity;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by leiyun on 2016/8/18.
 */
public class MyAccountActivity extends CustomTitleActivity {

    private ListView listView;
    private ArrayList<AccountItem> dataList = new ArrayList<AccountItem>();
    private TextView virtual_coin_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_account,R.layout.title_custom_red);
        setDefaultTitle("我的账户");


        getListData();

        virtual_coin_tv = (TextView) findViewById(R.id.virtual_coin_tv);

        listView = (ListView) findViewById(R.id.listView);
        setListView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getWXOrder(position);
            }
        });

        getMyVirtualCoin();

        onWXPayBack(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        onWXPayBack(intent);
    }

    private void onWXPayBack(Intent intent) {

        int resultCode = intent.getIntExtra(WXPayEntryActivity.Key_WXPayBackCode, -10);
        if(resultCode == -10){
            return;//此时还没有支付
        }

        if(resultCode == -2){
            Notice.alert(context,"用户取消");
        }else if(resultCode == -1){
            Notice.alert(context,"支付异常");
        }else if(resultCode == 0){
            Notice.alert(context,"充值成功!");
            getMyVirtualCoin();//刷新虚拟币
        }
    }
    private void getWXOrder(int position){
        HashMap<String,String> params = new HashMap<String, String>();
        params.put("total_fee",dataList.get(position).money+"");
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.wechatpay, params,2);
    }

    private void startWXPay(JsonObject params){
        IWXAPI api = WXAPIFactory.createWXAPI(this, AppConstants.WX_appid);
        api.registerApp(AppConstants.WX_appid);
        PayReq req = new PayReq();
        req.appId			= AppConstants.WX_appid;//json.getString("appid");
        req.partnerId		= params.get("partnerid").getAsString();
        req.prepayId		= params.get("prepayid").getAsString();
        req.nonceStr		= params.get("noncestr").getAsString();
        req.timeStamp		= params.get("timestamp").getAsString();
        req.packageValue	= "Sign=WXPay";//json.getString("package");
        req.sign			= params.get("sign").getAsString();
        req.extData			= "噔噔直播"; // optional
        Toast.makeText(this, "正在支付", Toast.LENGTH_SHORT).show();
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
    }
    private void getMyVirtualCoin(){
        requestData(NetTaskList.virtual_coin, AppInfo.getHeadInfo(),1);
    }
    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                if(id == 1){
                    String virtual_coin = response.data.getAsJsonObject().get("virtual_coin").getAsString();
                    LoginInfo.getInstance().setVirCoin(virtual_coin);//刷新内存虚拟币
                    virtual_coin_tv.setText(virtual_coin + "个虚拟货币");
                }else if(id == 2){
                    startWXPay(response.data.getAsJsonObject());
                }
            }
        }
        return false;
    }

    private void getListData(){
        AccountItem accountItem;

        accountItem = new AccountItem();
        accountItem.coin = 60;
        accountItem.coinGift = 0;
        accountItem.money = 6;
        dataList.add(accountItem);

        accountItem = new AccountItem();
        accountItem.coin = 300;
        accountItem.coinGift = 0;
        accountItem.money = 30;
        dataList.add(accountItem);

        accountItem = new AccountItem();
        accountItem.coin = 680;
        accountItem.coinGift = 0;
        accountItem.money = 68;
        dataList.add(accountItem);

        accountItem = new AccountItem();
        accountItem.coin = 980;
        accountItem.coinGift = 10;
        accountItem.money = 98;
        dataList.add(accountItem);

        accountItem = new AccountItem();
        accountItem.coin = 2980;
        accountItem.coinGift = 20;
        accountItem.money = 298;
        dataList.add(accountItem);

        accountItem = new AccountItem();
        accountItem.coin = 5880;
        accountItem.coinGift = 50;
        accountItem.money = 588;
        dataList.add(accountItem);
    }

    private View setListView() {

        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                RelativeLayout rl;
                rl = (RelativeLayout) View.inflate(context, R.layout.item_my_account, null);
                TextView coin_tv = (TextView) rl.findViewById(R.id.coin_tv);
                TextView coin_gift_tv = (TextView) rl.findViewById(R.id.coin_gift_tv);
                TextView money_tv = (TextView) rl.findViewById(R.id.money_tv);

                coin_tv.setText(dataList.get(position).coin+"个虚拟币");
                if(dataList.get(position).coinGift == 0){
                    coin_gift_tv.setText("");
                }else {
                    coin_gift_tv.setText("赠送"+dataList.get(position).coinGift+"个虚拟币");
                }
                money_tv.setText(dataList.get(position).money+"元");

                return rl;
            }
        });
        return listView;
    }

    private static class AccountItem{
        public int money;
        public int coin;
        public int coinGift;
    }
}
